/**
 *
 */
package com.zhp.fs.global;


import com.zhp.fs.utils.RequestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @ProjectName：zq-data-bank Maven Webapp
 * @PackageName：com.seentao.zqdata.core
 * @Description：
 * @Copyright：Copyright by SeenTao 2017年3月16日
 * @Company：SeenTao Co., Ltd.
 * @Author：likj
 * @Email：likja@seentao.com
 */
@ControllerAdvice
public class ExpController {

    private static final Log log = LogFactory.getLog("zqdata");

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exp(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        RequestUtils.doPrint(request);
        Map<String, Object> exMap = new HashMap<String, Object>();
        if (ex instanceof ParseException) {
            exMap.put("rtnCode", 0); // 1：成功 0：失败
            exMap.put("rtnMsg", "出现解析异常,操作失败.");
            exMap.put("stackMsg",
                    "异常请求：" + request.getRequestURL() + "," + ex.getMessage());
            log.error("异常请求：" + request.getRequestURL(), ex);
        } else if (ex instanceof MissingServletRequestParameterException) {
            exMap.put("rtnCode", 0); // 1：成功 0：失败
            exMap.put("rtnMsg", "参数缺失,操作失败.");
            exMap.put("stackMsg",
                    "异常请求：" + request.getRequestURL() + "," + ex.getMessage());
            log.error("异常请求：" + request.getRequestURL(), ex);
        } else if (ex instanceof TypeMismatchException) {
            exMap.put("rtnCode", 0); // 1：成功 0：失败
            exMap.put("rtnMsg", "参数类型不匹配,操作失败.");
            exMap.put("stackMsg",
                    "异常请求：" + request.getRequestURL() + "," + ex.getMessage());
            log.error("异常请求：" + request.getRequestURL(), ex);
        } else if (ex instanceof SQLException
                || ex instanceof TransientDataAccessResourceException
                || ex instanceof BadSqlGrammarException
                || ex instanceof UncategorizedSQLException) {
            exMap.put("rtnCode", 0); // 1：成功 0：失败
            exMap.put("rtnMsg", "sql异常,操作失败.");
            exMap.put("stackMsg",
                    "异常请求：" + request.getRequestURL() + "," + ex.getMessage());
            log.error("异常请求：" + request.getRequestURL(), ex);
        } else {
            exMap.put("rtnCode", 0); // 1：成功 0：失败
            exMap.put("rtnMsg", "未知异常,操作失败.");
            exMap.put("stackMsg",
                    "异常请求：" + request.getRequestURL() + "," + ex.getMessage());
            log.error("异常请求：" + request.getRequestURL(), ex);
        }
        return exMap;
    }

}
